﻿using ezCloud.Api.Models;
using ezCloud.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;


namespace ezCloud.Api.Common
{
    public class SMSMobifone
    {
        public static async Task sendSMSNotification(string msisdn,  string content)
        {
            string account = "0901800069";
            string password = "123456a@";
            string code = "125";
            string html = string.Empty;
            string url = @"http://onesms.mobifone.vn:8080/SMSAPI/SendSMS?msisdn="
                         + msisdn+"&account="+account+"&password="+password+"&content="+content+"&code="+code;
            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.AutomaticDecompression = DecompressionMethods.GZip;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                html = reader.ReadToEnd();
            }

        }
        public static async Task sendSMSNotificationBooked(ReservationRooms rr)
        {
            var user = Common.UserUtitily.getUserProfile();
            if (!InternalServices.IsInWhiteListHotels(user.HotelId.ToString())) { return; };
            string vn = "[HotelName] xin cảm ơn quý khách [GUESTNAME] đã sử dụng dịch vụ. Mã đặt phòng của quý khách là: [BOOKINGID]. Hãy liên hệ với chúng tôi để nhận được sự phục vụ tốt nhất [MOBILE] / [EMAIL] / [ADDRESS].";
            string en = "Thank and hope you enjoin using service of [HotelName]. Your booking information: Booking ID - [BOOKINGID] / Guest name - [GUESTNAME]. Please contact us [MOBILE] / [EMAIL] / [ADDRESS].";
            if (rr!=null)
            {
                string msisdn = "";
                Travellers traveller = new Travellers();
                if (rr.Travellers != null && rr.Travellers.Mobile != null)
                {
                    traveller = rr.Travellers;
                   
                }
                else
                {
                    ezCloudDataContext context = new ezCloudDataContext();
                    if (rr.TravellerId!=null)
                        traveller = context.Travellers.Where(t => t.TravellerId == rr.TravellerId).FirstOrDefault();
                }

                string content = "";
                string countryCode = "";
                string GuestName = "";
                if (traveller != null)
                {
                    if (traveller.Mobile == null) return;

                    msisdn = traveller.Mobile;

                    if (traveller.Fullname != null) GuestName = traveller.Fullname.TrimEnd(); 
                   
                    if (traveller.Countries != null && traveller.Countries.CountryCode != null)
                    {
                        countryCode = traveller.Countries.CountryCode;
                    }

                }
                else return;

                string hotelName = "";
                string hotelMobile = "";
                string hotelEmail = "";
                string hotelAddress = "";
                if (rr.Hotels != null && rr.Hotels.HotelName != null)
                {
                    hotelName = rr.Hotels.HotelName;
                    hotelMobile = rr.Hotels.HotelPhone == null ? "" : rr.Hotels.HotelPhone;
                    hotelEmail = rr.Hotels.HotelEmail == null ? "" : rr.Hotels.HotelEmail;
                    hotelAddress= rr.Hotels.HotelAddress == null ? "" : rr.Hotels.HotelAddress;
                }
                string reservationNumber = "";
                if (rr.Reservations != null && rr.Reservations.ReservationNumber != null) reservationNumber = rr.Reservations.ReservationNumber.ToString();

                vn = vn.Replace("[HotelName]", hotelName)
                        .Replace("[BOOKINGID]", reservationNumber)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail)
                        .Replace("[ADDRESS]", hotelAddress);

                en = en.Replace("[HotelName]", hotelName)
                        .Replace("[BOOKINGID]", reservationNumber)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail)
                        .Replace("[ADDRESS]", hotelAddress);

                content = countryCode == "VN" ? vn : en;
                if (msisdn.Length>9 && content.Length>0)
                        sendSMSNotification(msisdn, content);
            }


           
        }
        public static async Task sendSMSNotificationCheckin(ReservationRooms rr)
        {
            var user = Common.UserUtitily.getUserProfile();
            if (!InternalServices.IsInWhiteListHotels(user.HotelId.ToString())) { return; };
            string vn = " [HotelName]: Truy cập wifi MIỄN PHÍ tại khách sạn: [Wifi: bloomhotel], [mật khẩu:123123]. Chương trình ưu đãi cho khách lưu trú tại khách sạn giảm giá 30% massage (tầng 9). Hãy liên hệ với chúng tôi để nhận được sự phục vụ tốt nhất [MOBILE] / [EMAIL].";
            string en = " Welcome to [HotelName], [GUESTNAME]. You can access free wifi with information [wifi name:bloomhotel], [password:123123]. We have a special Offer and Discount 30% on massage service (9th floor). Please contact us [MOBILE] / [EMAIL].";
            if (rr != null)
            {
                string msisdn = "";
                Travellers traveller = new Travellers();
                if (rr.Travellers != null && rr.Travellers.Mobile != null)
                {
                    traveller = rr.Travellers;

                }
                else
                {
                    ezCloudDataContext context = new ezCloudDataContext();
                    if (rr.TravellerId != null)
                        traveller = context.Travellers.Where(t => t.TravellerId == rr.TravellerId).FirstOrDefault();
                }

                string content = "";
                string countryCode = "";
                string GuestName = "";
                if (traveller != null)
                {
                    if (traveller.Mobile == null) return;

                    msisdn = traveller.Mobile;

                    if (traveller.Fullname != null) GuestName = traveller.Fullname.TrimEnd();

                    if (traveller.Countries != null && traveller.Countries.CountryCode != null)
                    {
                        countryCode = traveller.Countries.CountryCode;
                    }

                }
                else return;

                string hotelName = "";
                string hotelMobile = "";
                string hotelEmail = "";
                if (rr.Hotels != null && rr.Hotels.HotelName != null)
                {
                    hotelName = rr.Hotels.HotelName;
                    hotelMobile = rr.Hotels.HotelPhone == null ? "" : rr.Hotels.HotelPhone;
                    hotelEmail = rr.Hotels.HotelEmail == null ? "" : rr.Hotels.HotelEmail;
                }
                string reservationNumber = "";
                if (rr.Reservations != null && rr.Reservations.ReservationNumber != null) reservationNumber = rr.Reservations.ReservationNumber.ToString();

                vn = vn.Replace("[HotelName]", hotelName)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail);

                en = en.Replace("[HotelName]", hotelName)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail);

                content = countryCode == "VN" ? vn : en;
                if (msisdn.Length > 9 && content.Length > 0)
                    sendSMSNotification(msisdn, content);
            }



        }

        public static async Task sendSMSNotificationCheckOut(ReservationRooms rr)
        {
            var user = Common.UserUtitily.getUserProfile();
            if (!InternalServices.IsInWhiteListHotels(user.HotelId.ToString())) { return; };
            string vn = "[HotelName] xin cảm ơn quý khách [GUESTNAME] đã sử dụng dịch vụ. Chúng tôi rất hân hạnh được phục vụ quý khách hàng. Hãy liên hệ với chúng tôi để nhận được sự phục vụ tốt nhất [MOBILE] / [EMAIL].";
            string en = "Thank and hope you enjoin using service of [HotelName]. We look forward to seeing you soon. Please contact us [MOBILE] / [EMAIL].";
            if (rr != null)
            {
                string msisdn = "";
                Travellers traveller = new Travellers();
                if (rr.Travellers != null && rr.Travellers.Mobile != null)
                {
                    traveller = rr.Travellers;

                }
                else
                {
                    ezCloudDataContext context = new ezCloudDataContext();
                    if (rr.TravellerId != null)
                        traveller = context.Travellers.Where(t => t.TravellerId == rr.TravellerId).FirstOrDefault();
                }

                string content = "";
                string countryCode = "";
                string GuestName = "";
                if (traveller != null)
                {
                    if (traveller.Mobile == null) return;

                    msisdn = traveller.Mobile;

                    if (traveller.Fullname != null) GuestName = traveller.Fullname.TrimEnd();

                    if (traveller.Countries != null && traveller.Countries.CountryCode != null)
                    {
                        countryCode = traveller.Countries.CountryCode;
                    }

                }
                else return;

                string hotelName = "";
                string hotelMobile = "";
                string hotelEmail = "";
                if (rr.Hotels != null && rr.Hotels.HotelName != null)
                {
                    hotelName = rr.Hotels.HotelName;
                    hotelMobile = rr.Hotels.HotelPhone == null ? "" : rr.Hotels.HotelPhone;
                    hotelEmail = rr.Hotels.HotelEmail == null ? "" : rr.Hotels.HotelEmail;
                }
                string reservationNumber = "";
                if (rr.Reservations != null && rr.Reservations.ReservationNumber != null) reservationNumber = rr.Reservations.ReservationNumber.ToString();

                vn = vn.Replace("[HotelName]", hotelName)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail);

                en = en.Replace("[HotelName]", hotelName)
                        .Replace("[GUESTNAME]", GuestName)
                        .Replace("[MOBILE]", hotelMobile)
                        .Replace("[EMAIL]", hotelEmail);

                content = countryCode == "VN" ? vn : en;
                if (msisdn.Length > 9 && content.Length > 0)
                    sendSMSNotification(msisdn, content);
            }



        }
    }
}